import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
var _excluded = ["label", "rules", "name", "children", "popoverProps"],
    _excluded2 = ["errorType", "rules", "name", "popoverProps", "children"];
import { jsxs as _jsxs, jsx as _jsx } from "react/jsx-runtime";
import { LoadingOutlined } from '@ant-design/icons';
import { ConfigProvider, Form, Popover } from 'antd-v4';
import { useContext, useEffect, useState } from 'react';
import './index.less';
var FIX_INLINE_STYLE = {
  marginTop: -5,
  marginBottom: -5,
  marginLeft: 0,
  marginRight: 0
};

var InlineErrorFormItem = function InlineErrorFormItem(_ref) {
  var inputProps = _ref.inputProps,
      input = _ref.input,
      extra = _ref.extra,
      errorList = _ref.errorList,
      popoverProps = _ref.popoverProps;

  var _useState = useState(false),
      _useState2 = _slicedToArray(_useState, 2),
      visible = _useState2[0],
      setVisible = _useState2[1];

  var _useState3 = useState([]),
      _useState4 = _slicedToArray(_useState3, 2),
      errorStringList = _useState4[0],
      setErrorList = _useState4[1];

  var _useContext = useContext(ConfigProvider.ConfigContext),
      getPrefixCls = _useContext.getPrefixCls;

  useEffect(function () {
    if (inputProps.validateStatus !== 'validating') {
      setErrorList(inputProps.errors);
    }
  }, [inputProps.errors, inputProps.validateStatus]);
  var prefixCls = getPrefixCls();
  return _jsx(Popover, _objectSpread(_objectSpread({
    trigger: (popoverProps === null || popoverProps === void 0 ? void 0 : popoverProps.trigger) || 'focus',
    placement: (popoverProps === null || popoverProps === void 0 ? void 0 : popoverProps.placement) || 'topRight',
    visible: errorStringList.length < 1 ? false : visible,
    onVisibleChange: function onVisibleChange(visibleParams) {
      if (visibleParams === visible) return;
      setVisible(visibleParams);
    },
    getPopupContainer: popoverProps === null || popoverProps === void 0 ? void 0 : popoverProps.getPopupContainer,
    getTooltipContainer: popoverProps === null || popoverProps === void 0 ? void 0 : popoverProps.getTooltipContainer,
    content: _jsxs("div", {
      className: "".concat(prefixCls, "-form-item-with-help"),
      children: [inputProps.validateStatus === 'validating' ? _jsx(LoadingOutlined, {}) : null, errorList]
    })
  }, popoverProps), {}, {
    children: _jsxs("div", {
      children: [input, extra]
    })
  }), "popover");
};

var InternalFormItem = function InternalFormItem(_ref2) {
  var label = _ref2.label,
      rules = _ref2.rules,
      name = _ref2.name,
      children = _ref2.children,
      popoverProps = _ref2.popoverProps,
      rest = _objectWithoutProperties(_ref2, _excluded);

  return _jsx(Form.Item, _objectSpread(_objectSpread({
    preserve: false,
    name: name,
    rules: rules,
    hasFeedback: true,
    // @ts-ignore
    _internalItemRender: {
      mark: 'pro_table_render',
      render: function render(inputProps, doms) {
        return _jsx(InlineErrorFormItem, _objectSpread({
          inputProps: inputProps,
          popoverProps: popoverProps
        }, doms));
      }
    }
  }, rest), {}, {
    style: _objectSpread(_objectSpread({}, FIX_INLINE_STYLE), rest === null || rest === void 0 ? void 0 : rest.style),
    children: children
  }));
};

export default (function (props) {
  var errorType = props.errorType,
      rules = props.rules,
      name = props.name,
      popoverProps = props.popoverProps,
      children = props.children,
      rest = _objectWithoutProperties(props, _excluded2);

  if (name && (rules === null || rules === void 0 ? void 0 : rules.length) && errorType === 'popover') {
    return _jsx(InternalFormItem, _objectSpread(_objectSpread({
      name: name,
      rules: rules,
      popoverProps: popoverProps
    }, rest), {}, {
      children: children
    }));
  }

  return _jsx(Form.Item, _objectSpread(_objectSpread({
    rules: rules
  }, rest), {}, {
    style: _objectSpread(_objectSpread({}, FIX_INLINE_STYLE), rest.style),
    name: name,
    children: children
  }));
});